﻿using Autofac;

using PDFtoImage;

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    internal class Program
    {
        static void Main(string[] args)
        {

            //IAnimal animal = new Tiger();
            //animal.Show("普通new()对象");

            var builder = new ContainerBuilder();
            var dependencyRegistrar = new DependencyRegistrar();//(IDependencyRegistrar)Activator.CreateInstance(typeof(IDependencyRegistrar));
            var container = dependencyRegistrar.Register( builder, null);

           

            var animalIOC = container.Resolve<Tiger>();
            //animalIOC.Show("Autofac方式实现new()对象");
            animalIOC.Show("阿");

            Console.WriteLine(animalIOC.Id);

            var animalIOC2 = container.Resolve<Dog>();
            //animalIOC2.Show("第二次从容器中实例化");
            Console.WriteLine(animalIOC2.Id);
            animalIOC2.Show("阿");
            Console.WriteLine("开启新的生命周期");
            ILifetimeScope inner = container.BeginLifetimeScope();
            var myClass3 = inner.Resolve<IAnimal>();
            Console.WriteLine(myClass3.Id);
            var myClass4 = inner.Resolve<IAnimal>();
            Console.WriteLine(myClass4.Id);



            //var animalIOC = container.Resolve<Dog>();
            //animalIOC.Show("Autofac方式实现new()对象");



            Console.ReadLine();
        }
    }
    public class Data
    {

        public string Name { get; set; }
    }
    public interface IAnimal
    {
        void Show(string name);
        string Id { get; set; }
    }
    public class Tiger : IAnimal
    {
        private string _Id;
        public string Id { get { return this._Id; } set { Id = this._Id; } }

        private Data _data { get; set; }
        public Tiger(Data data)
        {
            _data=  data; 
            _Id = Guid.NewGuid().ToString();
        }

        public void Show(string name)
        {
            _data.Name = "我是老虎";
            Console.WriteLine("老虎说：" + name);
        }
    }
    public class Dog : IAnimal
    {
        public string Id { get { return Guid.NewGuid().ToString(); } set { } }
        private Data _data { get; set; }

        public Dog(Data data)
        {
            _data = data;
        }
        public void Show(string name)
        {
           var res=  _data.Name;
            Console.WriteLine("狗狗说：" + name);
        }



    }
    public class DependencyRegistrar : IDependencyRegistrar
    {
        public int Order { get { return 0; } }

        public IContainer Register( ContainerBuilder builder, ITypeFinder typeFinder)
        {
            // InstancePerLifetimeScope 同一个Lifetime生成的对象是同一个实例
            // SingleInstance 单例模式，每次调用，都会使用同一个实例化的对象；每次都用同一个对象
            // InstancePerDependency 默认模式，每次调用，都会重新实例化对象；每次请求都创建一个新的对象
            builder.RegisterType<Tiger>().As<IAnimal>().AsImplementedInterfaces().InstancePerLifetimeScope();
            builder.RegisterType<Dog>().As<IAnimal>().InstancePerLifetimeScope();

            builder.RegisterType<Data>().InstancePerLifetimeScope();
            return builder.Build();

            //builder.RegisterType<Dog>().As<IAnimal>();//.PreserveExistingDefaults();
        }
    }

    public interface IDependencyRegistrar
    {
        IContainer Register(ContainerBuilder builder, ITypeFinder typeFinder);

        int Order { get; }
    }


    public interface ITypeFinder
    {
        IList<Assembly> GetAssemblies();

        IEnumerable<Type> FindClassesOfType(Type assignTypeFrom, bool onlyConcreteClasses = true);

        IEnumerable<Type> FindClassesOfType(Type assignTypeFrom, IEnumerable<Assembly> assemblies, bool onlyConcreteClasses = true);

        IEnumerable<Type> FindClassesOfType<T>(bool onlyConcreteClasses = true);

        IEnumerable<Type> FindClassesOfType<T>(IEnumerable<Assembly> assemblies, bool onlyConcreteClasses = true);
    }
}
